package com.mmz.test.activemq;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

public class JMSSender {
    public static void main(String[] args) throws JMSException {
        Session session = null;
        Connection con = null;
        try {
            // 通过工厂模式获取链接
            ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
            con = connectionFactory.createConnection();
            con.start();
            // 创建会话（是否使用事务，消息确认机制）
            session = con.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
            // 创建目的地（消息队列名称）
            Destination destination = session.createQueue("queue_text");
            // 创建生产者（消息发送器）
            MessageProducer producer = session.createProducer(destination);
            // 发送消息
            for (int i = 0; i < 3; i++) {
                TextMessage message = session.createTextMessage("你好旅行者" + i);
                message.setStringProperty("JMSXGroupID","even");
                Thread.sleep(1000);
                producer.send(message);
            }
            TextMessage message = session.createTextMessage("你好旅行者");
            producer.send(message);
//            for (int i = 0; i < 3; i++) {
//                TextMessage message = session.createTextMessage("你好旅行者" + i);
//                Thread.sleep(1000);
//                producer.send(message);
//            }
            session.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.close();
            con.close();
        }

    }
}
